// 简单UDP服务器
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#pragma comment(lib, "ws2_32.lib")

const int STRLENGTH = 20;

int main()
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(8080);

    int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    bind(s, (struct sockaddr *)&server, sizeof(server));
    printf("绑定成功！\n");

    struct sockaddr clientAddr;
    int clientAddrLen = sizeof(clientAddr);

    char buf[STRLENGTH] = {0};
    recvfrom(s, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&clientAddr, &clientAddrLen);
    sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));
    printf("%s\n", buf);

    closesocket(s);
    WSACleanup();
    return 0;
}
